from django.db import models


class Message(models.Model):
    message_id = models.BigAutoField(primary_key=True, db_comment='消息ID')
    title = models.CharField(max_length=200, db_comment='标题')
    content = models.TextField(db_comment='内容')
    message_type = models.CharField(max_length=50, db_comment='消息类型(系统通知/园区资讯/缴费提醒/活动推广/服务通知)')
    sender_user = models.ForeignKey('users.User', models.DO_NOTHING, blank=True, null=True, db_comment='发送者用户ID (管理员)')
    publish_time = models.DateTimeField(blank=True, null=True, db_comment='发布时间')
    valid_from = models.DateTimeField(db_comment='有效期开始')
    valid_until = models.DateTimeField(blank=True, null=True, db_comment='有效期结束')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        managed = False
        db_table = 'message'
        db_table_comment = '消息内容表: 存储消息的静态内容'


class MessageBroadcast(models.Model):
    broadcast_id = models.BigAutoField(primary_key=True, db_comment='广播ID')
    message = models.ForeignKey(Message, models.CASCADE, db_comment='关联的消息内容ID')
    target_criteria = models.JSONField(db_comment='接收目标筛选条件 (JSON格式)')
    status = models.CharField(max_length=20, default='PENDING', db_comment='广播状态(PENDING/PROCESSING/COMPLETED/FAILED)')
    processing_log = models.TextField(blank=True, null=True, db_comment='处理日志')
    created_at = models.DateTimeField(auto_now_add=True, db_comment='创建时间')
    completed_at = models.DateTimeField(blank=True, null=True, db_comment='完成时间')

    class Meta:
        managed = False
        db_table = 'message_broadcast'
        db_table_comment = '消息广播表: 定义一次消息的发送事件和目标受众'


class MessageReceipt(models.Model):
    receipt_id = models.BigAutoField(primary_key=True, db_comment='回执ID')
    message = models.ForeignKey(Message, models.CASCADE, db_comment='消息ID')
    broadcast = models.ForeignKey(MessageBroadcast, models.SET_NULL, blank=True, null=True, db_comment='来源广播ID (可选, 用于追溯)')
    user = models.ForeignKey('users.User', models.CASCADE, blank=True, null=True, db_comment='接收者用户ID')
    visitor = models.ForeignKey('users.Visitor', models.CASCADE, blank=True, null=True, db_comment='接收者访客ID')
    read_status = models.BooleanField(default=False, db_comment='是否已读')
    read_time = models.DateTimeField(blank=True, null=True, db_comment='阅读时间')
    is_deleted = models.BooleanField(default=False, db_comment='用户是否删除')
    created_at = models.DateTimeField(auto_now_add=True, db_comment='接收时间')

    class Meta:
        managed = False
        db_table = 'message_receipt'
        db_table_comment = '消息回执表: 记录每个用户/访客的消息接收、阅读状态'
        unique_together = (('message', 'user'), ('message', 'visitor'))


class Feedback(models.Model):
    feedback_id = models.BigAutoField(primary_key=True, db_comment='反馈ID')
    user = models.ForeignKey('users.User', models.SET_NULL, blank=True, null=True, db_comment='反馈用户ID (与访客ID二选一)')
    visitor = models.ForeignKey('users.Visitor', models.SET_NULL, blank=True, null=True, db_comment='反馈访客ID (与用户ID二选一)')
    title = models.CharField(max_length=200, db_comment='反馈标题')
    content = models.TextField(db_comment='反馈内容')
    media_urls = models.TextField(blank=True, null=True, db_comment='相关图片URL列表(JSON)')
    status = models.CharField(max_length=20, default='PENDING', db_comment='状态(PENDING/RESOLVED)')
    handler = models.ForeignKey('users.User', models.SET_NULL, blank=True, null=True, related_name='handled_feedbacks', db_comment='处理人用户ID')
    handle_result = models.TextField(blank=True, null=True, db_comment='处理结果')
    created_at = models.DateTimeField(auto_now_add=True, db_comment='提交时间')

    class Meta:
        managed = False
        db_table = 'feedback'
        db_table_comment = '问题反馈表: 允许注册用户或访客提交反馈'
        indexes = [
            models.Index(fields=['status'], name='idx_status')
        ]
